package com.example.taobao.ui.notifications;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.taobao.Huowu;
import com.example.taobao.R;
import com.example.taobao.bottomActivity;
import com.example.taobao.data.userdata;
import com.example.taobao.databinding.FragmentNotificationsBinding;
import com.example.taobao.personAdapte;
import com.example.taobao.ui.login.LoginActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;


    private TextView textView5;
    private Button button,JSButton;

    private CheckBox allCheckBox;
    private List<Huowu> HList = new ArrayList<>();
    private Boolean[] BooChecked = new Boolean[]{false,false,false,false,false,false};



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_notifications,null);
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

//        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
//        View root = binding.getRoot();

;
//        final TextView textView = binding.textNotifications;
//        ListView listview = view.findViewById(R.id.listviewN);
        ListView listView = view.findViewById(R.id.listviewN);

        textView5 = view.findViewById(R.id.tv_total_price);

        button = view.findViewById(R.id.tv_go_to_pay);

        allCheckBox = view.findViewById(R.id.all_chekbox);

        JSButton = view.findViewById(R.id.tv_delete);

        allCheckBox.setOnCheckedChangeListener(onCheckedChangeListener);


        BmobQuery<Huowu> bmobQuery1 = new BmobQuery<Huowu>();
        bmobQuery1.findObjects(new FindListener<Huowu>() {
            @Override
            public void done(List<Huowu> list, BmobException e) {
                if(e==null){
                    for (int i = 0 ;i<list.size();i++){
                        HList.add(list.get(i));
                    }
                    Log.d("path","查询成功");
                }
                else{
                    Log.d("path","查询不成功");
                }
            }
        });









        List<Map<String,Object>> listitem = new ArrayList<Map<String, Object>>();

//        List<Huowu> persons = new ArrayList<>();


        BmobQuery<Huowu> bmobQuery = new BmobQuery<Huowu>();
        bmobQuery.findObjects(new FindListener<Huowu>() {
            @Override
            public void done(List<Huowu> list, BmobException e) {
                if(e==null){
                    Log.d("path","查询成功");
                    personAdapte personAdapte = new personAdapte(getActivity(),R.layout.tiaomu,list);
                    listView.setAdapter(personAdapte);
                }
                else{
                    Log.d("path","查询不成功");
                }
            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                BmobQuery<Huowu> bmobQueryy = new BmobQuery<Huowu>();
                String str;
                Double Money;
                switch (i){
                    case 0:
                        bmobQuery.findObjects(new FindListener<Huowu>() {
                        @Override
                        public void done(List<Huowu> list, BmobException e) {
                            if(e==null){
                                Double Money1;
                                if(BooChecked[0]){
                                    BooChecked[0]=false;
                                    Toast.makeText(getActivity(), "取消选中", Toast.LENGTH_SHORT).show();
                                    Money1 = AllValue(BooChecked,HList);
                                    textView5.setText(Money1+"");
                                }else {
                                    BooChecked[0]=true;
//                                    Money = Money - gjiage;
                                    Toast.makeText(getActivity(), "已经选中", Toast.LENGTH_SHORT).show();
                                    Money1 = AllValue(BooChecked,HList);
                                    textView5.setText(Money1+"");
                                }

                            }
                        }
                    });
                        Money = AllValue(BooChecked,HList);
                        textView5.setText(Money+"");
                        break;
                    case 1:
                        bmobQuery.findObjects(new FindListener<Huowu>() {
                            @Override
                            public void done(List<Huowu> list, BmobException e) {
                                if(e==null){
                                    Double Money1;
                                    if(BooChecked[1]){
                                        BooChecked[1]=false;
                                        Toast.makeText(getActivity(), "取消选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }else {
                                        BooChecked[1]=true;
//                                    Money = Money - gjiage;
                                        Toast.makeText(getActivity(), "已经选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }
                                }
                            }
                        });
                        break;
                    case 2:
                        bmobQuery.findObjects(new FindListener<Huowu>() {
                            @Override
                            public void done(List<Huowu> list, BmobException e) {
                                if(e==null){
                                    Double Money1;
                                    if(BooChecked[2]){
                                        BooChecked[2]=false;
                                        Toast.makeText(getActivity(), "取消选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }else {
                                        BooChecked[2]=true;
//                                    Money = Money - gjiage;
                                        Toast.makeText(getActivity(), "已经选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }
                                }
                            }
                        });
                        break;
                    case 3:
                        bmobQuery.findObjects(new FindListener<Huowu>() {
                            @Override
                            public void done(List<Huowu> list, BmobException e) {
                                if(e==null){
                                    Double Money1;
                                    if(BooChecked[3]){
                                        BooChecked[3]=false;
                                        Toast.makeText(getActivity(), "取消选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }else {
                                        BooChecked[3]=true;
//                                    Money = Money - gjiage;
                                        Toast.makeText(getActivity(), "已经选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }
                                }
                            }
                        });
                        break;
                    case 4:
                        bmobQuery.findObjects(new FindListener<Huowu>() {
                            @Override
                            public void done(List<Huowu> list, BmobException e) {
                                if(e==null){
                                    Double Money1;
                                    if(BooChecked[4]){
                                        BooChecked[4]=false;
                                        Toast.makeText(getActivity(), "取消选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }else {
                                        BooChecked[4]=true;
//                                    Money = Money - gjiage;
                                        Toast.makeText(getActivity(), "已经选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }
                                }
                            }
                        });
                        break;
                    case 5:
                        bmobQuery.findObjects(new FindListener<Huowu>() {
                            @Override
                            public void done(List<Huowu> list, BmobException e) {
                                if(e==null){
                                    Double Money1;
                                    if(BooChecked[5]){
                                        BooChecked[5]=false;
                                        Toast.makeText(getActivity(), "取消选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }else {
                                        BooChecked[5]=true;
//                                    Money = Money - gjiage;
                                        Toast.makeText(getActivity(), "已经选中", Toast.LENGTH_SHORT).show();
                                        Money1 = AllValue(BooChecked,HList);
                                        textView5.setText(Money1+"");
                                    }
                                }
                            }
                        });
                        break;
                    default:
                        break;
                }
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                alert.setIcon(R.drawable.userimage);
                alert.setTitle("付款");
                alert.setMessage(textView5.getText().toString());
                alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                alert.create().show();
            }
        });

        JSButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });




        notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
//                textView.setText(s);
            }
        });
        return view;
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if(b){
                BmobQuery<Huowu> bmobQuery = new BmobQuery<Huowu>();
                bmobQuery.addQueryKeys("jiage");
                bmobQuery.findObjects(new FindListener<Huowu>() {
                    @Override
                    public void done(List<Huowu> list, BmobException e) {
                        Double j=0.0;
                        Log.d("all","统计成功1");
                        if(e==null){
                            for (int i = 0;i< list.size();i++){
                                j =j +list.get(i).getJiage();
                                BooChecked[i]=true;
                            }
                            textView5.setText(j+"");
                            Log.d("all","统计成功2");
                        }
                        else{
                            Log.d("all","统计不成功");
                        }
                    }
                });
            }else{
                for (int j = 0;j<BooChecked.length;j++){
                    BooChecked[j]=false;
                }
                textView5.setText("");
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    public Double AllValue(Boolean[] booChecked,List<Huowu> HList){
        Double str = 0.0;
        for (int i=0;i<HList.size();i++){

            if(booChecked[i]){
                str = str + HList.get(i).getJiage();
            }
        }
        return str;
    }


}